package com.basics.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author WangWei
 * @Date 2021/11/4 14:24
 * @Version 2.0
 * @Description
 **/
public class IdCardHttpUtils {
    private static final Logger log = LoggerFactory.getLogger(IdCardHttpUtils.class);

    public static String sendPostMap(String url, Map<String, String> parameters,String appcode) {
        String result = "";
        BufferedReader in = null;
        PrintWriter out = null;
        StringBuffer sb = new StringBuffer();
        String params = "";

        try {
            Iterator var7;
            String name;
            if (parameters.size() == 1) {
                var7 = parameters.keySet().iterator();

                while(var7.hasNext()) {
                    name = (String)var7.next();
                    sb.append(name).append("=").append(URLEncoder.encode((String)parameters.get(name), "UTF-8"));
                }

                params = sb.toString();
            } else if (parameters.size() > 0) {
                var7 = parameters.keySet().iterator();

                while(var7.hasNext()) {
                    name = (String)var7.next();
                    sb.append(name).append("=").append(URLEncoder.encode((String)parameters.get(name), "UTF-8")).append("&");
                }

                String temp_params = sb.toString();
                params = temp_params.substring(0, temp_params.length() - 1);
            }

            URL connURL = new URL(url);
            HttpURLConnection httpConn = (HttpURLConnection)connURL.openConnection();
            httpConn.setRequestProperty("Accept", "*/*");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
            httpConn.setRequestProperty("Authorization", "APPCODE " + appcode);
            httpConn.setRequestProperty("contentType", "application/x-www-form-urlencoded; charset=UTF-8");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            out = new PrintWriter(httpConn.getOutputStream());
            out.write(params);
            out.flush();

            String line;
            for(in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")); (line = in.readLine()) != null; result = result + line) {
            }
        } catch (Exception var18) {
            var18.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        return result;
    }
}
